package com.huaxia.day10;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.logging.FileHandler;

public class ExceptionHandleTest {
    public static void main(String[] args) throws Exception {
        System.out.println(parseInt("45"));
    }


    /**
     * 非运行异常
     */
    public static void readStream(String path) {
        try {
            FileInputStream fis = new FileInputStream(path);
        } catch (FileNotFoundException e) {

        } finally {

        }
    }

    public static void readStreamThrows(String path) throws FileNotFoundException {

        FileInputStream fis = new FileInputStream(path);

    }

    public static void readStreamThrow(String path) throws FileNotFoundException, FileFormatException, Exception {

        try {
            FileInputStream fis = new FileInputStream(path);
        } catch (FileNotFoundException ex) {
            // 自定义异常
            throw new FileFormatException(ex.getMessage(), ex.getStackTrace().toString());
        } catch (Exception e) {
            throw new Exception("未知异常");
        }

    }

    /**
     * 运行期异常
     * 重点关注
     * TODO 注意 finally的执行顺序
     */
    public static int parseInt(String number) {
        int res = 0;
        try {
            res = Integer.valueOf(number);
            return res;
        } catch (Exception e) {
            //返回改异常信息的详细描述
            System.out.println(e.getMessage());
            e.printStackTrace();// 将改异常的跟踪栈信息输出到标准错误输出
            return 1;
        } finally {
            res = 39;
            System.out.println("finally run");
            //  return 2;
        }
    }

    public static int parseIntException(String number) throws FileFormatException {
        int res = 0;
        try {
            res = Integer.valueOf(number);
            return res;
        } catch (Exception e) {
            throw new FileFormatException("自定义异常：" + e.getMessage(), e.getStackTrace().toString());
        } finally {

            System.out.println("finally run");
        }
    }

    /**
     * 获取自定义异常
     *
     * @param number
     */
    public static void printSelfEx(String number) {
        try {
            parseIntException(number);
        } catch (FileFormatException ex) {
            System.out.println(ex.getReason());
            System.out.println(ex.getExceptionDetail());
        }
    }

}
